package com.famdoc.Controller;




import com.famdoc.Exception.DownExcel;

import com.famdoc.Pojo.dto.CheckForm;
import com.famdoc.Pojo.dto.Result;
import com.famdoc.Pojo.entity.CheckItem;
import com.famdoc.Pojo.entity.Patient;
import com.famdoc.Service.PatientService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/patient")
public class PatientController {
    @Autowired
    private PatientService patientService;

    @PostMapping("/addCheckItem")
    public Result addFiles(@RequestBody CheckForm checkForm) {
        return patientService.addCheck(checkForm);
    }

    @PostMapping("/updateCheckItem")
    public Result addSings(@RequestBody CheckItem checkItem) {
        return patientService.updateCheckItem(checkItem);
    }

    @GetMapping("/selectCheckItem")
    public Result selectPatient(Integer page, Integer pageSize, Integer doctor_id, String name, String idNumber, String phoneNumber) {
        return patientService.selectPatient(page, pageSize, doctor_id, name, idNumber, phoneNumber);
    }

    @GetMapping("/count")
    public Result count(String diseaseName, String startingTime, String endingTime) {
        return patientService.count(diseaseName, startingTime, endingTime);
    }

    @GetMapping("/selectArchiveDetail")
    public Result selectArchiveDetail(Integer page, Integer pageSize, String diseaseName) {
        return patientService.selectArchiveDetail(page, pageSize, diseaseName);
    }

    @DeleteMapping("/deleteCheck")
    public Result deleteCheck(Integer checkItem_id) {
        return patientService.deleteCheck(checkItem_id);
    }

    @RequestMapping("/exportExcel")
    public void export(HttpServletResponse response) throws IllegalAccessException, IOException, InstantiationException {
        List<Patient> list = patientService.downloadPatientInfo();
        DownExcel.download(response,Patient.class,list);
    }

    @PostMapping("/importExcel")
    public Result importExcel(@RequestParam("file") MultipartFile file){
        List<Patient> list = null;
        try{
            System.out.println("患者档案上传中！");
            list = patientService.list(file.getInputStream());

        }catch (Exception e){
            return Result.error(201,"文件上传失败！");
        }
        return patientService.uploadPatientInfo(list);
    }
}


